package com.usian.controller;



import com.usian.feign.TeacherFeign;
import com.usian.pojo.Result;
import com.usian.pojo.Student;
import com.usian.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherFeign teacherFeign;

    @Autowired
    private LoadBalancerClient loadBalancerClient;  // 自动获取地址


    @PostMapping("add")
    public Result add(@RequestBody Student student){
        studentService.add(student);
        System.out.println(student.getName());
        //192.168.1.155
        ServiceInstance serviceInstance = loadBalancerClient.choose("spring-teacher");
        // http://192.168.1.155:8181/getUserById/1
        String url = serviceInstance.getUri()+"/teacher/update/"+student.getId()+"/"+student.getTid();
        Result result = teacherFeign.update(student.getId(), student.getTid());
        return result;
    }

    @RequestMapping("/addAll")
    public Result addAll(@RequestBody Student student){
            studentService.add(student);
        return new Result();
    }
    @RequestMapping("update/{id}")
    public Result update(@PathVariable Integer id){
        studentService.update(id);
        return new Result();
    }

    @GetMapping("/select/{id}")
    public List<Student> select(@PathVariable Integer id){
        return studentService.select(id);
    }

}
